# encoding=utf8  
import sys
sys.path.append("/work/opt/analytic/my_python/lib")
from lib.format_time import *
from lib.HandleMysql import *
from lib.switch_code import *
from jpype import *
import glob
import re
import os
reload(sys)
sys.setdefaultencoding('utf-8')

class Service58(object):
    def __init__(self):
        self.jars = ':'.join(glob.glob('./java_lib/umc/*.jar'))
	self.jvm_path = '/opt/soft/java/jre/lib/amd64/server/libjvm.so'
        startJVM(self.jvm_path,"-ea","-Djava.class.path=%s" % self.jars)
        self.SCFInit =JClass("com.bj58.spat.scf.client.SCFInit")
        self.SCFInit.init('./java_lib/umc/scf.config')
        
        print 'OK'
        self.UMC = JClass("com.bj58.ecdata.pyscf.service.UMCService")
        self.umcservice = self.UMC()
        
    def get_info(self,user_Id):
	#print self.umcservice.getTest()
        self.User =  self.umcservice.getUser(str(user_Id))
        #print self.User
        
        if self.User != None:
            self.Register = self.User.getRegister()

        #print self.User
        return self.User,self.Register
	#return None,None
    
    def shut_down(self):
        shutdownJVM()
    
 #mysql - stats
def set_open_conn():    
    conn = HandleMysql('10.5.20.16','58885','ec_stat','L#c8UM5RGh5C','dbwww58com_58_stats')
    conn.connect()
    return conn

#logic
def execute( param ):
    conn = set_open_conn();
    s58=Service58()
    stat_table = "t_user"
    
    query = "select user_id from user_id_tab order by user_id asc limit 10;"
    print query
    items = conn.get_results(query)
    for item in items:
        user_id = long(item[0])
        #print user_id
        try:
	    #print "test"
            user_info,reg_info= s58.get_info(user_id)
            
            #user_id = user_id
            user_name = user_info.getName()
            print user_name
            
            reg_time = reg_info.getTime()
            
            #print reg_time
            #param=[user_id,user_name,reg_time]
            #conn.insert_record(stat_table,param)
        except Exception,e:
            print e;
            
        #print user_id        
            
def main():

    #date = '2012-05-28'
   #execute(date)   
   if len(sys.argv)==1:
        start_date=get_yesterday()
        end_date=start_date
   elif len(sys.argv)==2:
        start_date=format_date(sys.argv[1])
        end_date=start_date
   elif len(sys.argv)==3:
        start_date=format_date(sys.argv[1])
        end_date=format_date(sys.argv[2])
   stat_date=start_date
   while stat_date>=start_date and stat_date<=end_date:
        print "The program of the date of %s is beginning at %s " %(stat_date,get_now())
        #
        execute( stat_date) 
        print "The program of the date of %s is end at %s " %(stat_date,get_now())
        stat_date=var_date(stat_date)

if __name__=='__main__':
    main()
